var ip = "127.0.0.1";

function networkingEngine() {
        this.ws = null;
        if(typeof MozWebSocket !== "undefined") {
                this.ws = new MozWebSocket('ws://'+ip+':8181/4911-game', 'post');
        }
        else {
                this.ws = new WebSocket('ws://'+ip+':8181/4911-game', 'post');
        }
        this.incomingOrders = [];
        this.outgoingOrders = [];

        this.ws.incomingOrders = this.incomingOrders;

        this.ws.onopen = function () {
                this.send(String.fromCharCode(messageTypes.initialGameState));
        };
        this.ws.onclose = function() {
        };
        this.ws.onerror = function (event) {
        };
        this.ws.onmessage = function (event) {
                var messageType=event.data.charCodeAt(0);
                switch(messageType){
                        case messageTypes.initialGameState:
                                game.initializeGameState(event.data.substr(1));
                                break;
                        case messageTypes.orders:
                                this.incomingOrders.push(event.data.substr(1));
                                console.log("incoming orders: " + game.networkingEngine.toCharCodes(event.data));
                                break;
                        case messageTypes.chat:
                                var chatLog = $('#chat_log');
                                chatLog.html(chatLog.html() + '<br/>' + event.data.substr(1));
                                chatLog.scrollTop(1000000);
                                break;
                }
        };
}

networkingEngine.prototype.queueBuildStructure = function(buildTypeId, tileX, tileY){
        var msgString=String.fromCharCode(orderTypes.buildStructure)+String.fromCharCode(buildTypeId)+String.fromCharCode(game.player.id)+String.fromCharCode(tileX)+String.fromCharCode(tileY);
        this.outgoingOrders.push(msgString);
}

networkingEngine.prototype.queueBuildUnit = function(buildFromId, buildTypeId){
        var msgString=String.fromCharCode(orderTypes.buildUnit)+String.fromCharCode(buildFromId)+String.fromCharCode(buildTypeId);
        this.outgoingOrders.push(msgString);
}

networkingEngine.prototype.queueCancelUnit = function(cancelFromId, index) {
	var msgString = String.fromCharCode(orderTypes.cancelUnit)+String.fromCharCode(cancelFromId)+String.fromCharCode(index);
	this.outgoingOrders.push(msgString);
}

networkingEngine.prototype.queueMove = function(x, y, selection){
        var msgString=String.fromCharCode(orderTypes.move)+String.fromCharCode(x)+String.fromCharCode(y)+String.fromCharCode(selection.length);

        for(var i=0;i<selection.length;i++){
                msgString+=String.fromCharCode(selection[i].id);
        }
        this.outgoingOrders.push(msgString);
}

networkingEngine.prototype.queueAttack = function(x, y, selection){
        var msgString=String.fromCharCode(orderTypes.attack)+String.fromCharCode(x)+String.fromCharCode(y)+String.fromCharCode(selection.length);

        for(var i=0;i<selection.length;i++){
                msgString+=String.fromCharCode(selection[i].id);
        }
        this.outgoingOrders.push(msgString);
}

networkingEngine.prototype.sendChatMessage = function() {
    var msg = $('#chat_input').val();
        if(msg.length > 0) {
                this.ws.send(String.fromCharCode(messageTypes.chat) + msg);
                $('#chat_input').val('');
        }
}

networkingEngine.prototype.dispatchOrders = function(frame, playerIndex, mode) {
        var outgoingOrders = String.fromCharCode(messageTypes.orders)+String.fromCharCode(frame)+String.fromCharCode(playerIndex);
        outgoingOrders += String.fromCharCode(this.outgoingOrders.length);
        while(this.outgoingOrders.length > 0) {
        if(this.outgoingOrders[0].charCodeAt(0) === orderTypes.buildUnit) {
            var derp = 0;
        }
                outgoingOrders += this.outgoingOrders.shift();
        }
        switch(mode) {
				case gameMode.replay:
					this.incomingOrders.push(localStorage.getItem('lastReplay'+frame));
					break;
                case gameMode.singlePlayer:
                        this.incomingOrders.push(outgoingOrders.substr(1));
                        console.log("incoming orders: " + this.toCharCodes(outgoingOrders));
                        break;
                case gameMode.multiPlayer:
                        this.ws.send(outgoingOrders);
                        break;
        }
}

networkingEngine.prototype.toCharCodes = function(data) {
        var retString = '';
        for(var i = 0; i < data.length; i++) {
                retString += data.charCodeAt(i) + ', ';
        }
        return retString;
}

var messageTypes = {
        initialGameState: 0,
        orders: 1,
        chat: 2
}

var orderTypes = {
    buildStructure: 0,
        buildUnit: 1,
		cancelUnit: 2,
        move: 3,
        attack: 4
}
